<template>
  <el-dialog
    title="添加话题分类"
    :visible="modalVisible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    class="topic-modal"
  >
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
    >
      <el-form-item
        label="话题分类名称"
        :label-width="formLabelWidth"
        prop="name"
      >
        <el-input
          v-model="ruleForm.name"
          auto-complete="off"
        ></el-input>
      </el-form-item>
    </el-form>
    <div
      slot="footer"
      class="dialog-footer"
    >
      <el-button @click="closeAddCateModal">取 消</el-button>
      <el-button
        type="primary"
        @click="handleSubmit('ruleForm')"
      >确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { mapMutations } from 'vuex'
export default {
  name: 'AddCategoryModal',
  data() {
    return {
      ruleForm: {
        name: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入话题分类名称', trigger: 'blur' }
        ]
      },
      formLabelWidth: '120px',
      uploading: false
    }
  },
  computed: {
    modalVisible() {
      return this.$store.state.topicMangeStore.addGateModalVisible
    }
  },
  methods: {
    handleClose() {
      this.closeAddCateModal()
    },
    //提交表单
    handleSubmit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.createTopicCategory()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    //创建分类
    createTopicCategory() {
      this.$post('trend/category', { name: this.ruleForm.name }).then(resp => {
        if (resp.message == 'OK') {
          this.$message({
            message: '话题分类创建成功',
            type: 'success'
          })
          this.closeAddCateModal()
          this.$emit('reload')
        }
      })
    },
    ...mapMutations(['closeAddCateModal'])
  }
}
</script>

<style>
</style>
